import json
from django.shortcuts import render
from rest_framework.decorators import api_view
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework_simplejwt.authentication import JWTAuthentication
from rest_framework.decorators import api_view, authentication_classes, permission_classes
from cinema.models import Cinema
from film.models import Film
from film.serializers import FilmListSerializer
from cinema.serializers import CinemaListSerializer
from .utils import is_15_minutes_passed
from .models import Order
from rest_framework import status


# Create your views here.

@api_view(['GET'])
@authentication_classes([JWTAuthentication])
@permission_classes([IsAuthenticated])
def order_all(request):
    try:
        params = int(request.GET.get('orderState'))
        print(type(params))
        user = request.user  # 这里就是当前认证的用户
        cinemas = Cinema.objects.all()
        print('cinmeas-type', type(cinemas))
        # serializer = CinemaListSerializern(cinmea, many=True)
        cinema_query_set = []
        film_query_set = []
        select_seat = []
        # cinema_serializers_list = []
        # film_serializers_list = []

        if not user:
            return Response({
                'state': 401,
                'msg': '用户未登录',
                'data': None
            }, status=status.HTTP_401_UNAUTHORIZED)

        order_ids = Order.objects.filter(user_id=user.id)
        for order_id in order_ids:
            cinema = Cinema.objects.get(id=order_id.cinema_id)
            film = Film.objects.get(id=order_id.film_id)
            if params == 0:  # 全部
                if cinema.seat:
                    seat = json.loads(cinema.seat)
                    for i_dict in seat:
                        if i_dict.get('userId') == user.id:
                            cinema_query_set.append(cinema)
                            select_seat.append(i_dict)
            if params == 1:  # 待付款
                if cinema.seat:
                    seat = json.loads(cinema.seat)
                    for i_dict in seat:
                        if i_dict.get('userId') == user.id and i_dict.get('payId') == 0:
                            cinema_query_set.append(cinema)
                            select_seat.append(i_dict)
            if params == 2:  # 待出票
                if cinema.seat:
                    seat = json.loads(cinema.seat)
                    for i_dict in seat:
                        if i_dict.get('userId') == user.id and i_dict.get('payId') != 0 and not is_15_minutes_passed(
                                i_dict.get('selectTime')):
                            cinema_query_set.append(cinema)
                            select_seat.append(i_dict)
            if params == 3:  # 已出票
                if cinema.seat:
                    seat = json.loads(cinema.seat)
                    for i_dict in seat:
                        if i_dict.get('userId') == user.id and i_dict.get('payId') != 0 and is_15_minutes_passed(
                                i_dict.get('selectTime')):
                            cinema_query_set.append(cinema)
                            select_seat.append(i_dict)

            film_query_set.append(film)
            # print(order_id.cinema_id)
            # print(order_id.film_id)

        cinema_serializers_list = CinemaListSerializer(cinema_query_set, many=True).data
        film_serializers_list = FilmListSerializer(film_query_set, many=True).data

        data_list = []
        for cinema, film, cinema_seat in zip(cinema_serializers_list, film_serializers_list, select_seat):
            data_dict = {
                'cinema': cinema,
                'film': film,
                'cinemaSeat': cinema_seat
            }
            data_list.append(data_dict)

        return Response({
            'state': 200,
            'msg': '请求成功',
            'data': data_list
        })
    except Exception as e:
        print('错误', e)
        return Response({
            'state': 200,
            'msg': f'错误：{e}',
            'data': []
        })
